package org.jeecg.modules.aspect;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.extbpm.process.pojo.RoleInfo;
import org.jeecg.modules.extbpm.process.pojo.UserInfo;
import org.jeecg.modules.extbpm.process.service.IExtActProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 	用户同步到工作流
 * @author zhoujf
 *
 */
@Aspect
@Slf4j
@Component("sysUserAspect")
public class SysUserAspect {
	
		@Autowired
		private ISysBaseAPI sysBaseAPI;
		@Autowired
		private IExtActProcessService extActProcessService;

	 	@Pointcut("execution(public * org.jeecg.modules.system.controller.SysUserController.add(..))")
	    public void excudeServiceAdd() {
	    }

	    @Around("excudeServiceAdd()")
	    public Object doAroundAdd(ProceedingJoinPoint pjp) throws Throwable {
	    	Object result = pjp.proceed();
	    	try {
				Object[] args = pjp.getArgs();
				JSONObject param =  (JSONObject)args[0];
				String userName = param.getString("username");
				log.info("------------SysUserAspect------add----"+userName);
				LoginUser user = sysBaseAPI.getUserByName(userName);
				if(user!=null) {
					if(CommonConstant.ACT_SYNC_1.equals(user.getActivitiSync())) {
						//同步则进行同步用户和角色信息
						UserInfo userInfo = new UserInfo();
						userInfo.setUuid(user.getId());
						userInfo.setFirstName(user.getRealname());
						userInfo.setEmail(user.getEmail());
						userInfo.setId(user.getUsername());
						List<RoleInfo> roleList = extActProcessService.getSysRolesByUserId(userInfo.getUuid());
						extActProcessService.saveActIdMembership(userInfo, roleList);
					}
				}
			} catch (Exception e) {
			}
	    	return result;
	    }
	    
	    @Pointcut("execution(public * org.jeecg.modules.system.controller.SysUserController.edit(..))")
	    public void excudeServiceEdit() {
	    }

	    @Around("excudeServiceEdit()")
	    public Object doAroundEdit(ProceedingJoinPoint pjp) throws Throwable {
	    	Object result = pjp.proceed();
	    	try {
				Object[] args = pjp.getArgs();
				JSONObject param =  (JSONObject)args[0];
				String userName = param.getString("username");
				log.info("------------SysUserAspect------edit----"+userName);
				LoginUser user = sysBaseAPI.getUserByName(userName);
				if(user!=null) {
					if(CommonConstant.ACT_SYNC_1.equals(user.getActivitiSync())) {
						//同步则进行同步用户和角色信息
						UserInfo userInfo = new UserInfo();
						userInfo.setUuid(user.getId());
						userInfo.setFirstName(user.getRealname());
						userInfo.setEmail(user.getEmail());
						userInfo.setId(user.getUsername());
						List<RoleInfo> roleList = extActProcessService.getSysRolesByUserId(userInfo.getUuid());
						extActProcessService.saveActIdMembership(userInfo, roleList);
					}else {
						//不同步删除用户信息
						extActProcessService.deleteActIdMembership(userName);
					}
				}
			} catch (Exception e) {
			}
	    	return result;
	    }


	@Pointcut("execution(public * org.jeecg.modules.system.controller.SysUserController.putRecycleBin(..))")
	public void excudeServicePutRecycleBin() {
	}

	@Around("excudeServicePutRecycleBin()")
	public Object doAroundPutRecycleBin(ProceedingJoinPoint pjp) throws Throwable {
		Object result = pjp.proceed();
		try {
			Object[] args = pjp.getArgs();
			JSONObject param = (JSONObject) args[0];
			String userIds = param.getString("userIds");
			log.info("------------SysUserAspect------PutRecycleBin----" + userIds);
			if (oConvertUtils.isNotEmpty(userIds)) {
				List<LoginUser> userList = sysBaseAPI.queryAllUserByIds((userIds.split(",")));
				for (LoginUser user : userList) {
					if (CommonConstant.ACT_SYNC_1.equals(user.getActivitiSync())) {
						//同步则进行同步用户和角色信息
						UserInfo userInfo = new UserInfo();
						userInfo.setUuid(user.getId());
						userInfo.setFirstName(user.getRealname());
						userInfo.setEmail(user.getEmail());
						userInfo.setId(user.getUsername());
						List<RoleInfo> roleList = extActProcessService.getSysRolesByUserId(userInfo.getUuid());
						extActProcessService.saveActIdMembership(userInfo, roleList);
					}
				}
			}
		} catch (Exception e) {
		}
		return result;
	}

	    @Pointcut("execution(public * org.jeecg.modules.system.controller.SysUserController.delete(..))")
	    public void excudeServiceDelete() {
	    }

	    @Around("excudeServiceDelete()")
	    public Object doAroundDelete(ProceedingJoinPoint pjp) throws Throwable {
	    	try {
				Object[] args = pjp.getArgs();
				String id =  (String)args[0];
				log.info("------------SysUserAspect------delete----"+id);
				LoginUser user = sysBaseAPI.getUserById(id);
				if(user!=null) {
					//不同步删除用户信息
					extActProcessService.deleteActIdMembership(user.getUsername());
				}
				
			} catch (Exception e) {
			}
	    	Object result = pjp.proceed();
	    	return result;
	    }
	    
	    @Pointcut("execution(public * org.jeecg.modules.system.controller.SysUserController.deleteBatch(..))")
	    public void excudeServiceDeleteBatch() {
	    }

	    @Around("excudeServiceDeleteBatch()")
	    public Object doAroundDeleteBatch(ProceedingJoinPoint pjp) throws Throwable {
	    	try {
				Object[] args = pjp.getArgs();
				String ids =  (String)args[0];
				log.info("------------SysUserAspect------deleteBatch----"+ids);
				String[] idArr = ids.split(",");
				for(String id :idArr) {
					LoginUser user = sysBaseAPI.getUserById(id);
					if(user!=null) {
						//不同步删除用户信息
						extActProcessService.deleteActIdMembership(user.getUsername());
					}
				}
			} catch (Exception e) {
			}
	    	Object result = pjp.proceed();
	    	return result;
	    }
}
